<?php 
/**
 * 品牌控制器
 */
class BrandController extends AuthController{
    /**
     * 
     */
    private $model;
	
	public function __auto(){
	    $this->model = K('Brand');
	}
	
	/**
	 * 首页
	 */
    public function index(){
    	$data = $this->model->all();
		//分配数据
		$this->assign('data',$data);
        $this->display(); 
    }
	
	/**
	 * 添加
	 */
	public function add(){
		if(IS_POST){
			if(!$this->model->addData()){
				$this->error($this->model->error);
			}
			$this->success('添加品牌成功', U('index'));
		}
        $this->display(); 
    }
	
	/**
	 * 编辑
	 */
	public function edit(){
		$bid = Q('get.bid',0,'intval');
		if(IS_POST){
			if(!$this->model->editDate($bid)){
				$this->error($this->model->error);
			}
			$this->success('修改成功', U('index'));
		}
		//获得旧数据
		$oldData = $this->model->where("bid={$bid}")->find();
		$this->assign('oldData',$oldData);
        $this->display(); 
    }
	
	/**
	 * 删除
	 */
	public function del(){
	    $bid = Q('get.bid',0,'intval');
		$info = $this->model->where("bid={$bid}")->find();
		if(is_file($info['logo'])) unlink($info['logo']);
		if(is_file($info['showimg'])) unlink($info['showimg']);
		//删除分类
		$this->model->where("bid={$bid}")->delete();
		//1.提示信息 2.跳转地址 3.跳转时间
		$this->success('删除成功',U('index'));
	}
	
	
	/**
	 * 删除图片
	 */
	public function delImg(){
	    if(IS_AJAX){
	    	//删除缩略图
	    	$path = Q('post.path');
			$bid = Q('post.bid',0,'intval');
			if(is_file($path)) unlink($path);
			//删除原图
			$path = str_repeat(C('THUMB_ENDFIX'), '', $path);
			if(is_file($path)) unlink($path);
			//删除数据库中对应文章的图片路径
			$this->model->where("bid={$bid}")->update(array('logo'=>''));
	    }
	}
	
	
	/**
	 * logo上传
	 */
	public function uploadLogo(){
		$upload = new Upload('Upload/Logo/' . date('y/m'));
        $file = $upload->upload();
        if (empty($file)) {
            $this->ajax('上传失败');
        } else {
        	//图片路径
            $data = $file[0];
            $this->ajax($data);
        }
	}
	
	
	
	/**
	 * 展示图上传
	 */
	public function uploadImg(){
		$upload = new Upload('Upload/Logo/' . date('y/m'));
        $file = $upload->upload();
        if (empty($file)) {
            $this->ajax('上传失败');
        } else {
        	//图片路径
            $data = $file[0];
            $this->ajax($data);
        }
	}
	
	
 }
 
 
 
 
 
 
 
 
 
 
 



 ?>